package com.cjjd.c.feature.mine.information;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.LogUtil;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.ToastUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/8/18.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class ReceiptAddFragment extends ActionBarFragment {


    @Bind(R.id.rb_company)
    RadioButton rbCompany;
    @Bind(R.id.rb_personal)
    RadioButton rbPersonal;
    @Bind(R.id.ll_tax_num)
    LinearLayout llTaxNum;
    @Bind(R.id.ll_compny_info)
    LinearLayout llCompnyInfo;
    @Bind(R.id.et_tax_name)
    EditText etTaxName;
    @Bind(R.id.et_tax_num)
    EditText etTaxNum;
    @Bind(R.id.et_company_address)
    EditText etCompanyAddress;
    @Bind(R.id.company_mobile)
    EditText companyMobile;
    @Bind(R.id.et_company_bank_name)
    EditText etCompanyBankName;
    @Bind(R.id.et_company_bank_num)
    EditText etCompanyBankNum;
    @Bind(R.id.et_tax_phone)
    EditText etTaxPhone;
    @Bind(R.id.et_tax_send_address)
    EditText etTaxSendAddress;
    @Bind(R.id.ttmc)
    TextView ttmc;
    @Bind(R.id.sh)
    TextView sh;
    @Bind(R.id.sjhm)
    TextView sjhm;
    @Bind(R.id.dz)
    TextView dz;


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        String str = "<font color= '#999999'>" + "抬头名称" + "</font>"
                + "<font color='#7a66cb'> *</font>";
        ttmc.setText(Html.fromHtml(str));

        String str2 = "<font color= '#999999'>" + "税         号" + "</font>"
                + "<font color='#7a66cb'> *</font>";
        sh.setText(Html.fromHtml(str2));

        String str3 = "<font color= '#999999'>" + "手机号码" + "</font>"
                + "<font color='#7a66cb'> *</font>";
        sjhm.setText(Html.fromHtml(str3));

        String str4 = "<font color= '#999999'>" + "邮寄地址" + "</font>"
                + "<font color='#7a66cb'> *</font>";
        dz.setText(Html.fromHtml(str4));

    }

    @Override
    public Object getTitleString() {
        return "新增发票信息";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_add_receipt;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    protected void initView() {
        rbCompany.setChecked(true);
        rbCompany.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    llTaxNum.setVisibility(View.VISIBLE);
                    llCompnyInfo.setVisibility(View.VISIBLE);
                }
            }
        });

        rbPersonal.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {
                    llTaxNum.setVisibility(View.GONE);
                    llCompnyInfo.setVisibility(View.GONE);
                }

            }
        });
    }

    @Override
    public void initRightButton(Button rightButton) {
        super.initRightButton(rightButton);
//        rightButton.setText("保存");
//        rightButton.setVisibility(View.VISIBLE);
    }


    @OnClick({R.id.rb_company, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.btn_next:
                if (checkInput()) addReceipt();
                break;


        }
    }

    private String taxName;
    private String dutyParagraph;
    private String address;
    private String telephone;
    private String bankName;
    private String bankCard;
    private String taxNum;

    private boolean checkInput() {
        taxName = etTaxName.getText().toString().trim();
        dutyParagraph = etTaxNum.getText().toString().trim();
        if (TextUtils.isEmpty(taxName)) {
            ToastUtil.showToast("请输入抬头名称");
            return false;
        }

        if (rbCompany.isChecked() && TextUtils.isEmpty(etTaxNum.getText().toString().trim())) {
            ToastUtil.showToast("请输入税号");
            return false;
        }
//
//        if (TextUtils.isEmpty(etTaxPhone.getText().toString().trim())) {
//            ToastUtil.showToast("请输入收件人手机号码");
//            return false;
//        }
//
//        if (TextUtils.isEmpty(etTaxSendAddress.getText().toString())) {
//            ToastUtil.showToast("请输入邮寄地址");
//            return false;
//        }

        return true;
    }


    private String invoiceType;  // 发票类型： 1个人 2公司

    private void addReceipt() {


        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));


//        h1.put("dutyParagraph", "5645661");             // 公司税号
//        h1.put("address", "北京市海淀区");            //公司地址
//        h1.put("telephone", "010-100000");          //公司电话
//        h1.put("bankName", "BOC");                  //开户银行
//        h1.put("bankCard", "56461563461561561");    //银行帐号
//        h1.put("mobile", "15500000009");          //手机号
//        h1.put("postAddress", "北京市海淀区"); //邮寄地址

        h1.put("dutyParagraph", etTaxNum.getText().toString());             // 公司税号
        h1.put("address", etCompanyAddress.getText().toString());           //公司地址
        h1.put("telephone", companyMobile.getText().toString());            //公司电话
        h1.put("bankName", etCompanyBankName.getText().toString());         //开户银行
        h1.put("bankCard", etCompanyBankNum.getText().toString());          //银行帐号
        //dada TODO 隐藏手机号跟地址 API需要调整一下啊
        h1.put("mobile", etTaxPhone.getText().toString().trim()+" 0");           //手机号
        h1.put("postAddress", etTaxSendAddress.getText().toString()+" 0");       //邮寄地址

        if (rbCompany.isChecked()) {
            LogUtil.e("xyd", "公司发票");
            invoiceType = "2";


        } else if (rbPersonal.isChecked()) {
            LogUtil.e("xyd", "个人发票");
            invoiceType = "1";

        }

        h1.put("invoiceType", invoiceType);              // 发票类型
        h1.put("company", taxName);                    // 发票抬头

        Call call = service.addMemberInvoice(JsonUtils.convertParam("addMemberInvoice", h1));
        call.enqueue(new CallbackImpl() {
            @Override
            public void responseSuccess(Call call, Response response) {
                ToastUtil.showToast("添加成功");
                mContext.onBackPressed();
            }
        });
    }
}
